using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[Serializable]
[PlayerStateScript(EPlayerState.Walk)]
public class PlayerMoveState : PlayerStateBase
{
    public PlayerMoveState(PlayerStateMachine sm) : base(sm) {}
    private static readonly int IsWalking = Animator.StringToHash("isWalking");


    public override void Update()
    {
       base.Update();
    }

    public override void FixedUpdate()
    {
        Vector2 velocity = stateMachine.playerInputDirction * stateMachine.speed;
        Move(velocity);
        
        if(!stateMachine.IsTouchingGround)
            stateMachine.SetState(EPlayerState.Fall);

    }

    public override void EnterState()
    {
        // Debug.Log("Enter Walk");
        stateMachine.playerComponentContex.animator.SetBool(IsWalking,true);
        PlayerAttribute.Instance.SetWalkPartivle(true);
    }

    public override void ExitState()
    {
        // Debug.Log("Exit Walk");
        stateMachine.playerComponentContex.animator.SetBool(IsWalking,false);
        PlayerAttribute.Instance.SetWalkPartivle(false);
    }
    

    public override void HandleInput(EPlayerInput playerInput, EInputType inputType)
    {
        switch (playerInput)
        {
            case EPlayerInput.Move:
                // if (inputType != EInputType.End)
                // {
                //     Vector2 velocity = stateMachine.playerInputDirction * stateMachine.speed;
                //     Move(velocity);
                // }
                if (inputType == EInputType.End)
                {
                    stateMachine.SetState(EPlayerState.Idle);
                }

                break;
            case EPlayerInput.Jump:
                if (inputType == EInputType.Begin)
                {
                    stateMachine.SetState(EPlayerState.Jump);
                }
                break;
            
            default:
                var idleState = stateMachine.GetPlayerState(EPlayerState.Idle);
                idleState.HandleInput(playerInput,inputType);
                break;
        }
    }
    
    private void Move(Vector2 velocity)
    {
        stateMachine.playerComponentContex.rigidbody.velocity = new Vector2(velocity.x,stateMachine.playerComponentContex.rigidbody.velocity.y) ;
    }
    
    
}
